package gold.digger;

import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC543 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int diameterOfBinaryTree(TreeNode root) {
            if (null == root) return 0;
            int rootDiameter = getMaxDepth(root.left) + getMaxDepth(root.right);
            int leftDiameter = diameterOfBinaryTree(root.left);
            int rightDiameter = diameterOfBinaryTree(root.right);
            return Math.max(Math.max(rootDiameter, leftDiameter), rightDiameter);
        }

        public int getMaxDepth(TreeNode root) {
            if (null == root) return 0;
            int leftDepth = getMaxDepth(root.left);
            int rightDepth = getMaxDepth(root.right);
            return Math.max(leftDepth, rightDepth) + 1;
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode root = TreeNode.createTree("[1,2,3,4,5]");
        System.out.println(solution.diameterOfBinaryTree(root));
    }

    public static void main(String[] args) throws Exception {
        LC543 an = new LC543();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
